<?php
if (!defined('__COLA_DIR__')) exit;
/**
 * 附件基类
 *
 * @author edikud
 * @package Widget
 * @copyright Copyright (c) 2019 Cola (http://www.mcooo.com)
 * @license GNU General Public License 2.0
 */

class Widget_Abstract_Attachs extends Widget
{

    /**
     * 获取查询对象
     *
     * @access public
     * @return Db_Query
     */
    public function select()
    {
        return $this->db->select()
		    ->from('table.attachs');
    }
	
	
    /**
     * 插入附件
     *
     * @access public
     * @param array $attachs 附件结构数组
     * @return integer
     */
    public function insert(array $values)
    {
        /** 构建插入结构 */
        $attach = array(
			'uid'           =>  isset($values['uid']) ? intval($values['uid']) : $this->user->uid,
            'id'            =>  empty($values['id']) ? 0 : intval($values['id']),
            'type'          =>  empty($values['type']) ? 'post' : $values['type'],
            'fileName'      =>  empty($values['fileName']) ? '' : $values['fileName'],
            'fileType'      =>  empty($values['fileType']) ? '' : $values['fileType'],			
            'fileSize'      =>  empty($values['fileSize']) ? 0 : intval($values['fileSize']),	
            'filePath'      =>  empty($values['filePath']) ? '' : $values['filePath'],			
            'created'       =>  empty($values['created']) ? $this->options->time : $values['created'],
            'isImage'       =>  !empty($values['isImage']) && 1 == $values['isImage'] ? 1 : 0,
        );

        if (!empty($values['aid'])) {
            $attach['aid'] = $values['aid'];
        }
		
		/** 插件接口 */
		//HOOK $attach | PLUG_Abstract_Attachs_Insert($values, $this);	

        return $this->db->insert('table.attachs')
		    ->values($attach)
			->lastInsertId();
    }

    /**
     * 更新附件
     *
     * @access public
     * @param array $aid 附件aid
     * @param array $attachs 附件结构数组
     * @return integer
     */
    public function update($aid, array $values)
    {
        /** 构建插入结构 */
        $update = array(
			'uid'           =>  isset($values['uid']) ? intval($values['uid']) : $this->user->uid,
            'id'            =>  empty($values['id']) ? 0 : intval($values['id']),
            'type'          =>  empty($values['type']) ? 'post' : $values['type'],
            'fileName'      =>  empty($values['fileName']) ? '' : $values['fileName'],
            'fileType'      =>  empty($values['fileType']) ? '' : $values['fileType'],			
            'fileSize'      =>  empty($values['fileSize']) ? 0 : intval($values['fileSize']),	
            'filePath'      =>  empty($values['filePath']) ? '' : $values['filePath'],			
            'created'       =>  empty($values['created']) ? $this->options->time : $values['created'],
            'isImage'       =>  !empty($values['isImage']) && 1 == $values['isImage'] ? 1 : 0,
        );

        $attach = array();
        foreach ($values as $key => $val) {
            if (array_key_exists($key, $update)) {
                $attach[$key] = $update[$key];
            }
        }
		
		/** 插件接口 */
		//HOOK $attach | PLUG_Abstract_Attachs_Update($values, $this);
		
        return $this->db->update('table.attachs')
		    ->where('aid = ?', $aid)
		    ->values($attach)
		    ->affectedRows();
    }

    /**
     * 删除附件
     *
     * @access public
     * @param int $aid 附件aid
     * @return integer
     */
    public function delete($aid)
    {
		/** 插件接口 */
		//HOOK PLUG_Abstract_Attachs_Delete($aid, $this);
		
		return $this->db->delete('table.attachs')
		    ->where('aid = ?', $aid)
			->affectedRows();
    }

    /**
     * 通用过滤器
     *
     * @access public
     * @param array $value 需要过滤的行数据
     * @return array
     * @throws Widget_Exception
     */
    public function filter(array $value)
    {
		/** 插件接口 */
		//HOOK $value | PLUG_Abstract_Attachs_Filter($value, $this);
		
        return $value;
    }

    /**
     * 将每行的值压入堆栈
     *
     * @access public
     * @param array $value 每行的值
     * @return array
     */
    public function push(array $value)
    {
        $value = $this->filter($value);
        return parent::push($value);
    }
}

